package com.wxq.auth.controller;

import com.fasterxml.jackson.core.type.TypeReference;
import com.wxq.auth.config.JwtProperties;
import com.wxq.auth.feign.MemberFeignService;
import com.wxq.auth.vo.UserLoginVO;
import com.wxq.common.utils.CookieUtils;
import com.wxq.common.utils.IpUtils;
import com.wxq.common.utils.JwtUtils;
import com.wxq.common.utils.R;
import com.wxq.common.vo.MemberVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;


/**
 * @author Jim
 * @date 5:03 下午 2022/4/12
 * @description 登录
 * @email:huwmz04153277@126.com
 */
@Controller
public class AuthController {

    @Autowired
    MemberFeignService memberFeignService;

    @Autowired
    private JwtProperties jwtProperties;

    /**
     *
     * @param account
     * @param password
     * @param returnUrl
     * @param request
     * @param response
     * @param redirectAttributes
     * @return
     */
    @PostMapping("ssoLogin")
    public String ssoLogin(@RequestParam("account") String account,
                           @RequestParam("password") String password,
                           @RequestParam("returnUrl") String returnUrl,
                           HttpServletRequest request,
                           HttpServletResponse response,
                           RedirectAttributes redirectAttributes) {
        UserLoginVO vo = new UserLoginVO();
        vo.setAccount(account);
        vo.setPassword(password);
        //调用远程接口
        R r = memberFeignService.login(vo);
        if (r.getCode() != 0){
            Map<String, String> errors = new HashMap<>();
            String msg = r.getData("mag", new TypeReference<String>() {});
            errors.put("msg",msg);
            redirectAttributes.addFlashAttribute("errors",errors);
            //登陆失败，重定向到登录页面
            return "redirect:http:auth.yomall.com/login2.html";
        }
        //3.把用户id及用户名放入载荷
        MemberVO memberVO = r.getData("data", new TypeReference<MemberVO>() {});
        Map<String, Object> map = new HashMap<>();
        map.put("userId",memberVO.getId());
        map.put("username",memberVO.getNickname());
        //4.为了防止jwt被盗取，载荷中加入用户ip地址
        String ipAddress = IpUtils.getIpAddress(request);
        map.put("ip",ipAddress);
        //5.制作jwt类型token信息
        try {
            String token = JwtUtils.generateToken(map,jwtProperties.getPrivateKey(), jwtProperties.getExpire());
            //6.把jwt放入cookie中
            CookieUtils.setCookie(request,response,jwtProperties.getCookieName(),token,jwtProperties.getExpire() * 60);
            //7.用户昵称放入cookie中，方便页面展示昵称
            CookieUtils.setCookie(request,response,jwtProperties.getUnick(),memberVO.getNickname(),jwtProperties.getExpire() * 60);

        } catch (Exception e ){
            e.printStackTrace();
        }
        return "redirect:" + returnUrl;

    }
}
